package com.fudan.backend.handler;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class IdTypeHandler extends BaseTypeHandler<String> {

    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, String s, JdbcType jdbcType) throws SQLException {
        preparedStatement.setLong(i, Long.parseLong(s));
    }

    @Override
    public String getNullableResult(ResultSet resultSet, String columnName) throws SQLException {
        long id = resultSet.getLong(columnName);
        return Long.toString(id);
    }

    @Override
    public String getNullableResult(ResultSet resultSet, int columnIndex) throws SQLException {
        long id = resultSet.getLong(columnIndex);
        return Long.toString(id);
    }

    @Override
    public String getNullableResult(CallableStatement callableStatement, int columnIndex) throws SQLException {
        long id = callableStatement.getLong(columnIndex);
        return Long.toString(id);
    }
}
